package com.example.a.handlerthread;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    private Button start = null;
    private Button end = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button)findViewById(R.id.start);
        start.setOnClickListener(new StartButtonListener());
        end = (Button)findViewById(R.id.end);
        end.setOnClickListener(new EndButtonListener());
        Log.e("tag", "UpdateThread");
    }
    Handler handler  = new Handler();
    class StartButtonListener implements OnClickListener{
        @Override
        public void onClick(View v) {
            handler.post(updateThread);
        }
    }
    class EndButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            handler.removeCallbacks(updateThread);
        }
    }
    Runnable updateThread =  new Runnable(){
        @Override
        public void run() {
            System.out.println("UpdateThread");
            Log.v("tag", "UpdateThread");
            handler.postDelayed(updateThread, 2000);
        }

    };
}
